﻿using In66.Net.EventBus.RabbitMq.Enums;

namespace In66.Net.EventBus.RabbitMq.Attributes;

/// <summary>
/// 死信队列配置
/// <a href="https://www.rabbitmq.com/dlx.html">Dead Letter Exchanges</a>
/// </summary>
[AttributeUsage(AttributeTargets.Class)]
public sealed class DeadLetterAttribute : Attribute
{
    private EWorkModel _workModel { get; set; }

    private string _exchangeName { get; set; }

    private string _routingKey { get; set; }

    private string _queue { get; set; }

    private bool _enable { get; set; }


    public DeadLetterAttribute(EWorkModel workModel, string exchangeName = "", string routingKey = "", string queue = "", bool enable = true)
    {
        _workModel = workModel;
        _exchangeName = exchangeName;
        _routingKey = routingKey;
        _queue = queue;
        _enable = enable;

    }

    /// <summary>
    /// 交换机名称
    /// </summary>
    public string ExchangeName => _workModel switch
    {
        EWorkModel.PublishSubscribe => string.IsNullOrWhiteSpace(_exchangeName) ? "xdl.amq.fanout" : _exchangeName,
        EWorkModel.Routing => string.IsNullOrWhiteSpace(_exchangeName) ? "xdl.amq.direct" : _exchangeName,
        EWorkModel.Topics => string.IsNullOrWhiteSpace(_exchangeName) ? "xdl.amq.topic" : _exchangeName,
        _ => ExchangeNameCheck(_exchangeName)
    };

    /// <summary>
    /// 交换机模式
    /// </summary>
    public EWorkModel WorkModel => _workModel;

    /// <summary>
    /// 路由键《路由键和队列名称配合使用》
    /// </summary>
    public string RoutingKey => _workModel switch
    {
        EWorkModel.None => _queue,
        _ => _routingKey
    };

    /// <summary>
    /// 队列名称《队列名称和路由键配合使用》
    /// </summary>
    public string Queue => _queue;

    /// <summary>
    /// 是否启用队列
    /// </summary>
    public bool Enable => _enable;

    private static string ExchangeNameCheck(string exchangeName)
    {
        if (string.IsNullOrWhiteSpace(exchangeName))
        {
            throw new ArgumentNullException(nameof(exchangeName));
        }

        return exchangeName;
    }
}